﻿using HardcoreGame.Common.Players;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class HungerSystem : ModSystem
    {
        private Texture2D[] HungerTextures;
        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int HungerLayerIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Inventory"));
            if (HungerLayerIndex != -1)
            {
                layers.Insert(HungerLayerIndex, new LegacyGameInterfaceLayer(
                    "HardcoreGame: Hunger",
                    delegate
                    {
                        DrawHungerUI();
                        return true;
                    },
                    InterfaceScaleType.UI));
            }
        }
        public void DrawHungerUI()
        {
            HungerTextures = new Texture2D[10];
            for (int i = 0; i < 10; i++)
            {
                HungerTextures[i] = ModContent.Request<Texture2D>($"HardcoreGame/Assets/Textures/Hunger/Hunger{i + 1}").Value;
            }
            Player player = Main.player[Main.myPlayer];
            int stage = player.GetModPlayer<HungerPlayer>().StageForHunger;
            Color color = Color.White;
            int sX = Main.screenWidth - 800;
            int maxStage = 10;
            if (!Main.ingameOptionsWindow)
            {
                stage = Utils.Clamp(stage, 1, 10);
                Main.spriteBatch.Draw(HungerTextures[stage - 1], new Vector2(sX + 362, 85), color);
                Vector2 mousePosition = new(Main.mouseX, Main.mouseY);
                Rectangle drawRect = new(sX + 362, 85, HungerTextures[stage - 1].Width, HungerTextures[stage - 1].Height);
                if (drawRect.Contains((int)mousePosition.X, (int)mousePosition.Y))
                {
                    int reversedStage = maxStage - stage + 1;
                    string text = $"{reversedStage}/{maxStage}";
                    Vector2 textPosition = mousePosition + new Vector2(40, 40);
                    Color textColor = Color.White;

                    Utils.DrawBorderString(Main.spriteBatch, text, textPosition, textColor, 1f, 0.5f, 0.5f);
                }
            }
        }
    }
}
